package com.ruoyi.framework.web.service;

import com.ruoyi.common.core.domain.DTO.ProductBO;
import com.ruoyi.common.core.domain.DTO.ProductSaveBO;
import com.ruoyi.common.core.domain.entity.Products;
import com.ruoyi.system.mapper.ProductMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

@Component
@Transactional
public class ProductService {
    @Autowired
    private ProductMapper productMapper;
    //插入商品
    public void insertProduct(ProductSaveBO productSaveBO){
        Products products = new Products();
        BeanUtils.copyProperties(productSaveBO, products);
        products.setCreatedAt(new Date());
        products.setUpdatedAt(new Date());
        productMapper.insertProduct(products);
    }

    //修改商品
    public void updateProduct(Products products){
        if(ObjectUtils.isEmpty(products.getId())){
            throw new RuntimeException("商品ID不能为空");
        }
        productMapper.updateProduct(products);
    }

    //查询商品列表
    public List<Products> selectProductList(ProductBO productBO){
        return productMapper.selectProductList(productBO.getName(),productBO.getStatus());
    }

    //根据商品id获取商品信息
    public Products selectProductById(Long id){
        return productMapper.selectProductById(id);
    }
}
